package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.service.FormLabelService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formlabel.dto.FormLabelListDTO;
import com.xbongbong.pro.formlabel.vo.FormLabelListVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 系统标签相关接口
 * @author 魏荣杰
 *
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/formLabel")
public class MobileFormLabelController {

    @Resource
    private FormLabelService formLabelService;

    /**
     * Description: 系统标签列表（组件选择用）
     * @param formLabelListDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 19:27
     * @since
     */
    @RequestMapping(value = "/sampleList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sampleList(@RequestBody @Valid FormLabelListDTO formLabelListDTO , BindingResult br) throws Exception {
        XbbResponse<FormLabelListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormLabelListVO formLabelListVO = formLabelService.sampleList(formLabelListDTO);
                response = new XbbResponse<>(formLabelListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 标签列表
     * @param formLabelListDTO
     * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 19:25
     * @since
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid FormLabelListDTO formLabelListDTO, BindingResult br) throws Exception {
        XbbResponse<FormLabelListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormLabelListVO formLabelListVO = formLabelService.list(formLabelListDTO);
                response = new XbbResponse<>(formLabelListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: tab页设置标签的标签列表
     * @param formLabelListDTO
     * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 19:25
     * @since
     */
    @RequestMapping(value = "/tab/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String tabList(@RequestBody @Valid FormLabelListDTO formLabelListDTO, BindingResult br) throws Exception {
        XbbResponse<FormLabelListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormLabelListVO formLabelListVO = formLabelService.tabList(formLabelListDTO);
                response = new XbbResponse<>(formLabelListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

}
